package com.zr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zr.constant.MessageConstant;
import com.zr.entity.Result;
import com.zr.pojo.OrderSetting;
import com.zr.service.OrderSettingService;
import com.zr.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @auther zhaorui
 * @date 2019-11-27 - 20:43
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //调用POI工具类  中的readExcel方法 读取上传的excelFile 文件
            List<String[]> list = POIUtils.readExcel(excelFile);
            //判断list是否为空
            if (list != null && list.size() > 0){
                //list中是string[]  数组 在这里需要转换成 ordersetting对象
                //首先定义一个泛型为OrderSetting的list集合
                List<OrderSetting> orderSettingList = new ArrayList<>();
                //遍历集合
                for (String[] strings : list) {
                    //将list中的string[] 转成ordersetting对象
                    //将strings 中的数据 添加到orderSetting 对象中
                    //strings中的两个元素 分别为 date类型 和 integer类型
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                    //将ordersetting对象添加到orderSettingList中  也就是将工具类读取Excel文件的结果集 转换成泛型为OrderSetting的结果集
                    orderSettingList.add(orderSetting);
                }
                //调用service中的add方法 将结果导入到数据库中
                orderSettingService.add(orderSettingList);
            }

            return Result.ok(MessageConstant.IMPORT_ORDERSETTING_SUCCESS,list);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期查询 预约信息
     * @param date
     * @return
     */
    @RequestMapping("/findOrderSetting")
    public Result findOrderSetting(String date){//date:2019-11
        try {
            //调用
            List<Map> list = orderSettingService.findOrderSetting(date);
            return Result.ok(MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {

            return Result.error(MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editByOrderDate")
    //public Result editByOrderDate(Integer number,String orderDate){
    public Result editByOrderDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editByOrderDate(orderSetting);
            return Result.ok(MessageConstant.ORDERSETTING_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
